package com.shaoshaohuo.app.ui;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.News;
import com.shaoshaohuo.app.entity.NewsEntity;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.adapter.NewsAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

public class NewsActivity extends BaseActivity implements IXListViewListener {

	private TopbarView mTopbarView;

	private XListView mListview;
	
	private List<News> mDataList = new ArrayList<News>();
	private int size = 10;
	private String action = ListViewAction.NEW;
	private String cursor = "";

	private NewsAdapter<News> mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news);
		initView();
		setUpView();
		requestData(true);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mListview = (XListView) this.findViewById(R.id.listview_data_list);
	}

	private void setUpView() {
		mTopbarView.setCenterText("新闻");
		mTopbarView.setLeftView(true, true);

		mListview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = (News) parent.getItemAtPosition(position);
                Intent aboutIntent = new Intent(NewsActivity.this,WebBrowserActivity.class);
                aboutIntent.putExtra(WebBrowserActivity.TITLE, news.getTitle());
                aboutIntent.putExtra(WebBrowserActivity.URL, news.getShowurl());
                startActivity(aboutIntent);
            }
        });
		setListView();
	}
	
	private void requestData(boolean isLoading) {
		if(isLoading){
			startLoadingDialog();
		}
		RequestService.getInstance().getNewsList(this, cursor, size+"", action, "", NewsEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					NewsEntity entity = (NewsEntity) resultData;
					List<News> list = entity.getData().getList();
					mDataList.addAll(list);
					
					if ((mDataList.size() < size) || (action.equals(ListViewAction.HISTORY1) && list.size() < size)) {
//						mListview.setPullLoadEnable(false);
						mListview.endLoadingMore("没有更多了");
					}
					setAdapter(mDataList);
				}else{
					showToast(resultData.getMsg());
				}
				onLoad();
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
				onLoad();
			}
		});
	}
	
	protected void setAdapter(List<News> mDataList2) {
		if(mAdapter == null){
			mAdapter = new NewsAdapter<News>(this, mDataList2, false);
			mListview.setAdapter(mAdapter);
		}else{
			mAdapter.setData(mDataList2);
		}
	}

	private void setListView() {
		mListview.setPullLoadEnable(true);
		mListview.setPullRefreshEnable(true);
		mListview.setXListViewListener(this);
		
//		mListview.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
////				Coupon item = (Coupon) parent.getItemAtPosition(position);
////				mAdapter.setSelectPosition(position);
////				showToast(item.getTitle());
////				Intent intent = new Intent(MessageCenterActivity.this,OrderDetailActivity.class);
////				intent.putExtra(ExtraName.orderid, order.getBusinessorderid());
////				startActivity(intent);
//			}
//		});
	}
	
	@Override
	public void onRefresh() {
		if(mDataList.size()>0){
			cursor = mDataList.get(0).getCursor();
		}else {
			cursor = "";
		}
		action = ListViewAction.NEW;
		mListview.setPullLoadEnable(true);
		requestData(false);
	}

	@Override
	public void onLoadMore() {
		if(mDataList.size()>0){
			cursor = mDataList.get(mDataList.size()-1).getCursor();
		}else {
			cursor = "";
		}
		action = ListViewAction.HISTORY;
		requestData(false);
	}

	private void onLoad() {
		mListview.stopRefresh();
		mListview.stopLoadMore();
//		mListview.setRefreshTime(StringHelper.formatDate(System.currentTimeMillis()+""));
	}
	
}
